#!/usr/bin/perl

use strict;

package main;

use Getopt::Long;
use List::Util qw(first);
use Lunmgr ':all';
use Data::Dumper;

my %_opts;

GetOptions(\%_opts,
          "-all",
          "-help|h");

my $devtable = readDevtable("/home/lunmgr/lunmgr.devtable");
my @pathes = split(/,/, join(',', @ARGV));
my @entries = @$devtable if ($_opts{all});

if (! $_opts{all} && @pathes > 0) {
    foreach my $path (@pathes) {
        my $entry = first {$_->path eq $path} @$devtable;

        $entry ? push @entries, $entry
               : print STDERR "Invalid path $path\n";
    }
}

foreach my $entry (@entries) {

    my $model = $entry->model; $model =~ s/_/ /g;

    my $msa = getMsaForWwn($entry->wwn);

    next unless ($msa);

    print "\n  Device name: " . $entry->name 
          . ", mounted under " . $entry->mount_pt . "\n",
          "  ----------------------------------------------------------\n",
          "  Unique ID: " . $entry->uuid . "\n",
          "  Lun " . $entry->lun . " on $model (IP: " . $msa->ips->[0] 
          . " [" . $msa->name . "])\n";

    my $mpath = getMpathForUuid($entry->uuid); 
    if ($mpath) {
        my $dev = getDevForUuid($entry->uuid);
        my @devs = split(',', $dev);
        my $pstatus = join(', ', map { $_ . "->" . getStatusForDev($_) } @devs);

        print "  DM-Multipath alias: $mpath, with paths [$dev]\n",
          "  Path status: $pstatus\n";
    }
    else  {
        print "  DM-Multipath alias:  - DEVICE NOT VISIBLE\n";
    }
}
print "\n";
